﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraEditors;
using NNS.Enumeration;
using NNS.UI;
using QLTB.BusinessLayer;
using QLTB.Common;

namespace QLTB.Nhom.UI
{
    public partial class frmNhom : XtraForm
    {
        private int type;

        public frmNhom()
        {
            InitializeComponent();

            ucNhom.NhomChanged += ucNhom_NhomChanged;
            ucQuyen.SaveChanged += ucQuyen_SaveChanged;
            ucNguoiDung.Choosed += ucNguoiDung_Choosed;
            ucNhom.Choosed += ucNhom_Choosed;
            TBL_quyenBL.IgnoreError = true;
            if (!TBL_quyenBL.isXem(EnumComponent.iUser))
            {
                dpuser.Visibility = DockVisibility.Hidden;
            }
            TBL_quyenBL.IgnoreError = false;
        }

        private frmMain MyRibbon
        {
            get { return ParentForm as frmMain; }
        }

        private void ucNhom_Choosed(object sender, NhomEventArgs e)
        {
            //throw new NotImplementedException();
            type = e.Id;
        }

        private void ucNguoiDung_Choosed(object sender, NhomEventArgs e)
        {
            type = e.Id;
        }


        private void ucQuyen_SaveChanged(object sender, SaveChangedEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.NotSave)
            {
                Text = "Phân quyền người dùng (*)";
                //this.Appearance.ForeColor = Color.Red;
            }
            else
            {
                Text = "Phân quyền người dùng";
            }
        }

        private void ucNhom_NhomChanged(object sender, NhomEventArgs e)
        {
            //throw new NotImplementedException();
            ucQuyen.Nap(e.Id);
            ucQuyen.Caption = e.Tennhom;
            ucNguoiDung.Idnhom = e.Id;
        }

        private void frmNhom_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = !Program._exit;
        }

        public void ExecuteMode(EditMode e)
        {
            if (e == EditMode.SaveClose)
            {
                ucNhom.Save();
            }
        }

        public void Add()
        {
            ucNguoiDung.Add();
        }

        public void Delete()
        {
            if (type == 0)
            {
                MyBox.ShowWarning("Vui lòng chọn dữ liệu cần xóa!", "Phân quyền người dùng");
            }
            else if (type == 1)
            {
                ucNhom.Deleted();
            }
            else
            {
                ucNguoiDung.Deleted();
            }
            //ucNguoiDung.Add();
        }

        public void Nap()
        {
            ucNhom.Nap();
            ucQuyen.Nap();
            ucNguoiDung.Nap();
        }

        public void Cancel()
        {
            ucQuyen.NotSave = false;
            ucQuyen.Nap();
        }

        public void Save()
        {
            ucQuyen.Save();
            //ucQuyen.Nap();
        }

        private void frmNhom_Activated(object sender, EventArgs e)
        {
            if (MyRibbon != null)
            {
                MyRibbon.IgClose.Visible = true;
                MyRibbon.iEdit.Visible = false;
                MyRibbon.iUndo.Visibility = BarItemVisibility.Always;
                MyRibbon.iCancel.Visibility = BarItemVisibility.Always;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iSave.Visibility = BarItemVisibility.Always;
                MyRibbon.iClose.Visibility = BarItemVisibility.Always;
                MyRibbon.iRefesh.Visibility = BarItemVisibility.Always;

                MyRibbon.iCopy.Visibility = BarItemVisibility.Never;
                MyRibbon.iSelectAll.Visibility = BarItemVisibility.Never;
                MyRibbon.iClear.Visibility = BarItemVisibility.Never;
                MyRibbon.iDelete.Visibility = BarItemVisibility.Always;

                AddEvent();
            }
        }

        private void AddEvent()
        {
            MyRibbon.IAdd.ItemClick += IAdd_ItemClick;
            MyRibbon.iClose.ItemClick += iClose_ItemClick;
            MyRibbon.iRefesh.ItemClick += iRefesh_ItemClick;
            MyRibbon.iCancel.ItemClick += iCancel_ItemClick;
            MyRibbon.iSave.ItemClick += iSave_ItemClick;
            MyRibbon.iDelete.ItemClick += iDelete_ItemClick;
        }

        private void iDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            Delete();
        }

        private void iSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            Save();
        }

        private void iCancel_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            Cancel();
        }

        private void iRefesh_ItemClick(object sender, ItemClickEventArgs e)
        {
            Nap();
        }

        private void iClose_ItemClick(object sender, ItemClickEventArgs e)
        {
            Close();
        }

        private void IAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            ucNguoiDung.Add();
        }

        private void frmNhom_Deactivate(object sender, EventArgs e)
        {
            if (MyRibbon != null)
            {
                MyRibbon.IgClose.Visible = false;
                MyRibbon.iEdit.Visible = false;
                MyRibbon.iUndo.Visibility = BarItemVisibility.Never;
                MyRibbon.iCancel.Visibility = BarItemVisibility.Never;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iSave.Visibility = BarItemVisibility.Never;
                MyRibbon.iClose.Visibility = BarItemVisibility.Never;
                MyRibbon.iRefesh.Visibility = BarItemVisibility.Never;

                MyRibbon.iCopy.Visibility = BarItemVisibility.Never;
                MyRibbon.iSelectAll.Visibility = BarItemVisibility.Never;
                MyRibbon.iClear.Visibility = BarItemVisibility.Never;
                MyRibbon.iDelete.Visibility = BarItemVisibility.Never;

                RemoveEvent();
            }
        }

        private void RemoveEvent()
        {
            MyRibbon.IAdd.ItemClick -= IAdd_ItemClick;
            MyRibbon.iClose.ItemClick -= iClose_ItemClick;
            MyRibbon.iRefesh.ItemClick -= iRefesh_ItemClick;
            MyRibbon.iCancel.ItemClick -= iCancel_ItemClick;
            MyRibbon.iSave.ItemClick -= iSave_ItemClick;
            MyRibbon.iDelete.ItemClick -= iDelete_ItemClick;
        }
    }
}